package com.wcl.demo.ui.activity;

import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.easemob.chat.EMChatManager;
import com.wcl.demo.R;
import com.wcl.demo.db.UserInforHelper;
import com.wcl.demo.model.entity.UserInfor;
import com.wcl.demo.network.VolleySingleton;
import com.wcl.demo.utils.PreferenceManager;
import com.wcl.demo.view.CircleImage;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by dllo on 15/11/21.
 */
public class UserInforActivity extends BaseActivity {

    private boolean flag = true;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();//定义Volley请求
    private String username = EMChatManager.getInstance().getCurrentUser();

    @OnClick(R.id.activity_userinfor_bac_Iv)
    void activity_userinfor_bac_Iv() {
        if (user_infor_finishBtn.getText().equals("编辑")) {
            finish();
        } else {
            Toast.makeText(this, "信息尚未保存,如需退出请先点击'保存'", Toast.LENGTH_SHORT).show();
        }
    }

    @Bind(R.id.user_edit_person)
    EditText user_edit_person;
    @Bind(R.id.user_edit_work)
    EditText user_edit_work;
    @Bind(R.id.user_edit_school)
    EditText user_edit_school;
    @Bind(R.id.user_edit_birthday)
    EditText user_edit_birthday;
    @Bind(R.id.user_edit_phone)
    EditText user_edit_phone;
    @Bind(R.id.user_edit_area)
    EditText user_edit_area;
    @Bind(R.id.user_infor_finishBtn)
    Button user_infor_finishBtn;
    @Bind(R.id.user_person_finish)
    TextView user_person_finish;
    @Bind(R.id.user_work_finish)
    TextView user_work_finish;
    @Bind(R.id.user_school_finish)
    TextView user_school_finish;
    @Bind(R.id.user_birthday_finish)
    TextView user_birthday_finish;
    @Bind(R.id.user_telephone_finish)
    TextView user_telephone_finish;
    @Bind(R.id.user_area_finish)
    TextView user_area_finish;
    @Bind(R.id.user_infor_finish)
    FrameLayout user_infor_finish;
    @Bind(R.id.user_infor_edit)
    FrameLayout user_infor_edit;
    @Bind(R.id.user_infor_user_name_finish)
    TextView user_infor_user_name_finish;
    @Bind(R.id.user_infor_head_image_finish)
    CircleImage user_infor_head_image_finish;

    private UserInforHelper helper;
    private String user_person, user_work, user_school, user_birthday, user_phone, user_area, user_name;

    @OnClick(R.id.user_infor_finishBtn)
    void onclickBtn() {

        if (!flag) {

            user_infor_finishBtn.setText("编辑");
            user_infor_finish.setVisibility(View.VISIBLE);
            user_infor_edit.setVisibility(View.INVISIBLE);
            //从textView获取值设置到editText上
//            user_edit_person.setText(user_person_finish.getText());
//            user_edit_work.setText(user_work_finish.getText());
//            user_edit_school.setText(user_school_finish.getText());
//            user_edit_birthday.setText(user_birthday_finish.getText());
//            user_edit_phone.setText(user_telephone_finish.getText());
//            user_edit_area.setText(user_area_finish.getText());

            user_person = user_edit_person.getText().toString();
            user_work = user_edit_work.getText().toString();
            user_school = user_edit_school.getText().toString();
            user_birthday = user_edit_birthday.getText().toString();
            user_phone = user_edit_phone.getText().toString();
            user_area = user_edit_area.getText().toString();
            user_name = user_infor_user_name_finish.getText().toString();
            helper.deleteAll();

            List<UserInfor> infors = new ArrayList<>();
            UserInfor infor = new UserInfor();
            infor.setUsername(user_name);
            infor.setUser_person(user_person);
            infor.setUser_work(user_work);
            infor.setUser_school(user_school);
            infor.setUser_birthday(user_birthday);
            infor.setUser_phone(user_phone);
            infor.setUser_parent_area(user_area);
            infors.add(infor);
            helper.saveUserInfor(infors);
            Log.i("kjljdkfkjlfdfgfff", infors + "");

            if (helper != null) {
                List<UserInfor> list = helper.queryAll();
                Log.i("kjljdkfkjl", list + "");
                user_person_finish.setText(list.get(0).getUser_person());
                user_work_finish.setText(list.get(0).getUser_work());
                user_school_finish.setText(list.get(0).getUser_school());
                user_birthday_finish.setText(list.get(0).getUser_birthday());
                user_telephone_finish.setText(list.get(0).getUser_phone());
                user_area_finish.setText(list.get(0).getUser_parent_area());
            }

        } else {
            user_infor_finishBtn.setText("保存");
            user_infor_finish.setVisibility(View.INVISIBLE);
            user_infor_edit.setVisibility(View.VISIBLE);

            List<UserInfor> list = helper.queryAll();
            if (list != null && list.size() > 0) {
                user_edit_person.setText(list.get(0).getUser_person());
                user_edit_work.setText(list.get(0).getUser_work());
                user_edit_school.setText(list.get(0).getUser_school());
                user_edit_birthday.setText(list.get(0).getUser_birthday());
                user_edit_phone.setText(list.get(0).getUser_phone());
                user_edit_area.setText(list.get(0).getUser_parent_area());
            }
        }
        flag = !flag;
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_user_infor;
    }

    @Override
    protected void initView() {
        helper = new UserInforHelper();
        user_infor_user_name_finish.setText(username);
        getBitmap(getCurrentUserAvatar() , user_infor_head_image_finish);
    }

    @Override
    protected void initData() {
        setText();

    }

    public void setText() {
        List<UserInfor> list = helper.queryAll();
        if (list != null && list.size() > 0) {
//            List<UserInfor> list = helper.queryAll();
            Log.i("kjljdkfkjl", list + "");
            user_person_finish.setText(list.get(0).getUser_person());
            user_work_finish.setText(list.get(0).getUser_work());
            user_school_finish.setText(list.get(0).getUser_school());
            user_birthday_finish.setText(list.get(0).getUser_birthday());
            user_telephone_finish.setText(list.get(0).getUser_phone());
            user_area_finish.setText(list.get(0).getUser_parent_area());
        }

    }
    private String getCurrentUserNick() {
        return PreferenceManager.getInstance().getCurrentUserNick();
    }

    private String getCurrentUserAvatar() {
        return PreferenceManager.getInstance().getCurrentUserAvatar();
    }


    /***
     * 图片请求
     *
     * @param bitmap
     * @param imageView
     */
    private void getBitmap(String bitmap, final ImageView imageView) {
        volleySingleton.getImageResuest(bitmap, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }
}

